`timescale 1ns / 1ps


module MAX9979_SPI_Control(
	input 				 clk,
	input  		[1:0]    adate_select,
	input 				 spi_reset,
	input 				 spi_start,
	output reg 			 spi_sdi,
	input 				 spi_sdo,
	output reg 			 sclk,
	output reg [3:0] 	 cs,
	input      [23:0]    data,
	output reg           complete,
	output reg [23:0] 	 sdo_para_data
    );
    reg [31:0]   ss;
    reg [7:0]    s;
    reg [3:0]    cs_temp;
    always @(posedge clk)
    begin
        if(spi_start == 0)
        begin
            s <= 0;
            complete <= 0;
        end
        else if((spi_start == 1) && (s < 72))
        begin
            s <= s + 1;
            complete <= 0;
        end
        else begin
            s <= s;
            complete <= 1;
        end          
    end
    
    always @(posedge clk)
    begin
        case(adate_select)
            2'b00  :   cs_temp <= 4'b1110;
            2'b01  :   cs_temp <= 4'b1101;
            2'b10  :   cs_temp <= 4'b1011;
            2'b11  :   cs_temp <= 4'b0111;
           default :   cs_temp <= 4'b0000;
        endcase
    end
    
    always @(posedge clk)
    begin
        if(spi_start == 0)
        begin
            ss <= data;
            spi_sdi <= 0;
            sclk <= 0;
            cs <= 4'b1111;        
        end
        else begin
             case(s)
                4 : begin  spi_sdi<=1'b0; cs<=4'b1111;sclk<=0; end
                5 : begin  spi_sdi<=1'b0; cs<=4'b1111;sclk<=1; end
                6 : begin  spi_sdi<=1'b0; cs<=4'b1111;sclk<=0; end
                7 : begin  spi_sdi<=1'b0; cs<=4'b1111;sclk<=1; end
                8 : begin  spi_sdi<=1'b0; cs<=4'b1111;sclk<=0; end
                9 : begin  spi_sdi<=1'b0; cs<=4'b1111;sclk<=1; end
                10 : begin  spi_sdi<=1'b0; cs<=4'b1111;sclk<=0; end
                11 : begin  spi_sdi<=1'b0; cs<=4'b1111;sclk<=1; end
                12 : begin  spi_sdi<=1'b0; cs<=4'b1111;sclk<=0; end
                13 : begin  spi_sdi<=1'b0; cs<=4'b1111;sclk<=1; end
                14 : begin  spi_sdi<=1'b0; cs<=4'b1111;sclk<=0; end
                15 : begin  spi_sdi<=1'b0; cs<=4'b1111;sclk<=1; end
                16 : begin  spi_sdi<=1'b0; cs<=4'b1111;sclk<=0; end
                17 : begin  spi_sdi<=1'b0; cs<=4'b1111;sclk<=1; end
                18 : begin spi_sdi<=ss[23]; cs<=cs_temp;sclk<=0; end
                19 : begin spi_sdi<=ss[23]; cs<=cs_temp;sclk<=1; sdo_para_data <= {sdo_para_data[22:0],spi_sdo}; end
                20 : begin spi_sdi<=ss[22]; cs<=cs_temp;sclk<=0; end
                21 : begin spi_sdi<=ss[22]; cs<=cs_temp;sclk<=1; sdo_para_data <= {sdo_para_data[22:0],spi_sdo}; end
                22 : begin spi_sdi<=ss[21]; cs<=cs_temp;sclk<=0; end
                23 : begin spi_sdi<=ss[21]; cs<=cs_temp;sclk<=1; sdo_para_data <= {sdo_para_data[22:0],spi_sdo}; end
                24 : begin spi_sdi<=ss[20]; cs<=cs_temp;sclk<=0; end
                25 : begin spi_sdi<=ss[20]; cs<=cs_temp;sclk<=1; sdo_para_data <= {sdo_para_data[22:0],spi_sdo}; end
                26 : begin spi_sdi<=ss[19]; cs<=cs_temp;sclk<=0; end
                27 : begin spi_sdi<=ss[19]; cs<=cs_temp;sclk<=1; sdo_para_data <= {sdo_para_data[22:0],spi_sdo}; end
                28 : begin spi_sdi<=ss[18]; cs<=cs_temp;sclk<=0; end
                29 : begin spi_sdi<=ss[18]; cs<=cs_temp;sclk<=1; sdo_para_data <= {sdo_para_data[22:0],spi_sdo}; end
                30 : begin spi_sdi<=ss[17]; cs<=cs_temp;sclk<=0; end
                31 : begin spi_sdi<=ss[17]; cs<=cs_temp;sclk<=1; sdo_para_data <= {sdo_para_data[22:0],spi_sdo}; end
                32 : begin spi_sdi<=ss[16]; cs<=cs_temp;sclk<=0; end
                33 : begin spi_sdi<=ss[16]; cs<=cs_temp;sclk<=1; sdo_para_data <= {sdo_para_data[22:0],spi_sdo}; end
                34 : begin spi_sdi<=ss[15]; cs<=cs_temp;sclk<=0; end
                35 : begin spi_sdi<=ss[15]; cs<=cs_temp;sclk<=1; sdo_para_data <= {sdo_para_data[22:0],spi_sdo}; end
                36 : begin spi_sdi<=ss[14]; cs<=cs_temp;sclk<=0; end
                37 : begin spi_sdi<=ss[14]; cs<=cs_temp;sclk<=1; sdo_para_data <= {sdo_para_data[22:0],spi_sdo}; end
                38 : begin spi_sdi<=ss[13]; cs<=cs_temp;sclk<=0; end
                39 : begin spi_sdi<=ss[13]; cs<=cs_temp;sclk<=1; sdo_para_data <= {sdo_para_data[22:0],spi_sdo}; end
                40 : begin spi_sdi<=ss[12]; cs<=cs_temp;sclk<=0; end
                41 : begin spi_sdi<=ss[12]; cs<=cs_temp;sclk<=1; sdo_para_data <= {sdo_para_data[22:0],spi_sdo}; end
                42 : begin spi_sdi<=ss[11]; cs<=cs_temp;sclk<=0; end
                43 : begin spi_sdi<=ss[11]; cs<=cs_temp;sclk<=1; sdo_para_data <= {sdo_para_data[22:0],spi_sdo}; end
                44 : begin spi_sdi<=ss[10]; cs<=cs_temp;sclk<=0; end
                45 : begin spi_sdi<=ss[10]; cs<=cs_temp;sclk<=1; sdo_para_data <= {sdo_para_data[22:0],spi_sdo}; end
                46 : begin spi_sdi<=ss[9]; cs<=cs_temp;sclk<=0; end
                47 : begin spi_sdi<=ss[9]; cs<=cs_temp;sclk<=1; sdo_para_data <= {sdo_para_data[22:0],spi_sdo}; end
                48 : begin spi_sdi<=ss[8]; cs<=cs_temp;sclk<=0; end
                49 : begin spi_sdi<=ss[8]; cs<=cs_temp;sclk<=1; sdo_para_data <= {sdo_para_data[22:0],spi_sdo}; end
                50 : begin spi_sdi<=ss[7]; cs<=cs_temp;sclk<=0; end
                51 : begin spi_sdi<=ss[7]; cs<=cs_temp;sclk<=1; sdo_para_data <= {sdo_para_data[22:0],spi_sdo}; end
                52 : begin spi_sdi<=ss[6]; cs<=cs_temp;sclk<=0; end
                53 : begin spi_sdi<=ss[6]; cs<=cs_temp;sclk<=1; sdo_para_data <= {sdo_para_data[22:0],spi_sdo}; end
                54 : begin spi_sdi<=ss[5]; cs<=cs_temp;sclk<=0; end
                55 : begin spi_sdi<=ss[5]; cs<=cs_temp;sclk<=1; sdo_para_data <= {sdo_para_data[22:0],spi_sdo}; end
                56 : begin spi_sdi<=ss[4]; cs<=cs_temp;sclk<=0; end
                57 : begin spi_sdi<=ss[4]; cs<=cs_temp;sclk<=1; sdo_para_data <= {sdo_para_data[22:0],spi_sdo}; end
                58 : begin spi_sdi<=ss[3]; cs<=cs_temp;sclk<=0; end
                59 : begin spi_sdi<=ss[3]; cs<=cs_temp;sclk<=1; sdo_para_data <= {sdo_para_data[22:0],spi_sdo}; end
                60 : begin spi_sdi<=ss[2]; cs<=cs_temp;sclk<=0; end
                61 : begin spi_sdi<=ss[2]; cs<=cs_temp;sclk<=1; sdo_para_data <= {sdo_para_data[22:0],spi_sdo}; end
                62 : begin spi_sdi<=ss[1]; cs<=cs_temp;sclk<=0; end
                63 : begin spi_sdi<=ss[1]; cs<=cs_temp;sclk<=1; sdo_para_data <= {sdo_para_data[22:0],spi_sdo}; end
                64 : begin spi_sdi<=ss[0]; cs<=cs_temp;sclk<=0; end
                65 : begin spi_sdi<=ss[0]; cs<=cs_temp;sclk<=1; sdo_para_data <= {sdo_para_data[22:0],spi_sdo}; end
                66 : begin  spi_sdi<=1'b0; cs<=4'b1111;sclk<=0; end
                67 : begin  spi_sdi<=1'b0; cs<=4'b1111;sclk<=1; end
                68 : begin  spi_sdi<=1'b0; cs<=4'b1111;sclk<=0; end
                69 : begin  spi_sdi<=1'b0; cs<=4'b1111;sclk<=1; end
                70 : begin  spi_sdi<=1'b0; cs<=4'b1111;sclk<=0; end
                71 : begin  spi_sdi<=1'b0; cs<=4'b1111;sclk<=1; end
                
                default : begin spi_sdi<=0;cs<=4'b1111;sclk<=0;end
            endcase
        end
    end
endmodule

